package com.cw.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.common.api.ResultObject;
import com.cw.dto.OneToManyDto;
import com.cw.dto.PageDto;
import com.cw.dto.PersonChangreDto;
import com.cw.dto.UserDeptPostPojo;
import com.cw.entity.SysRole;
import com.cw.entity.SysUser;
import com.cw.entity.UserDeptPost;
import com.cw.mapper.SysUserRoleMapper;
import com.cw.mapper.UserDeptPostMapper;
import com.cw.service.ISysRoleService;
import com.cw.service.ISysUserService;
import com.cw.service.IUserDeptPostService;
import com.cw.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户、部门、职务 服务实现类
 * </p>
 *
 * @author smq
 * @since 2021-04-28
 */
@Service
public class UserDeptPostServiceImpl extends ServiceImpl<UserDeptPostMapper, UserDeptPost> implements IUserDeptPostService {
    @Resource
    private UserDeptPostMapper userDeptPostMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleDao;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;
    /**
     * 功能描述: <br>
     * 〈添加部门人员〉
     *
     * @Param: [oneToManyDto]
     * @Return: com.srm.util.ResultObject
     * @Author: wzw
     * @Date: 2019/11/11 15:46
     * @see ISysUserService
     */
    @Override
    public ResultObject insertDeptUser(OneToManyDto addDeptAndUserIdDto) {
        String deptId = addDeptAndUserIdDto.getDeptId();
        if (deptId != null) {
            //先删除后添加
            //因 有角色的用户不会出现在，用户添加列表里 如下代码作废

            HashMap<String, Object> map = new HashMap<>(5);
            map.put("D_ID", deptId);
            userDeptPostMapper.deleteByMap(map);

            List<String> userList = addDeptAndUserIdDto.getUserId();
            if (userList != null && userList.size() != 0) {
                for (String s : userList) {
                    userDeptPostMapper.insertRoleAndUser(UUIDUtil.uuid(), addDeptAndUserIdDto.getDeptId(), s);
                }
                return ResultObject.successReturn(null);
            }
        } else {
            return ResultObject.errorReturn("部门id不能为空");
        }
        return null;
    }

    @Override
    public int insertList(List<UserDeptPost> list) {
        return userDeptPostMapper.insertList(list);
    }

    @Override
    public List<UserDeptPostPojo> selectAll(String uId, String dId, String pId) {
        return userDeptPostMapper.selectAll( uId,dId,pId);
    }

    @Override
    public List<String> getDepsName(String userId) {
        return userDeptPostMapper.getDepsName(userId);
    }

    @Override
    public int deleteUserDeptPost(String userId,String deptId) {
        return userDeptPostMapper.deleteUserDeptPost(userId,deptId);
    }

    /**
     * 功能描述: <br>
     * 〈添加部门和人员的关系〉
     *
     * @Param: [addDeptAndUserIdDto]
     * @Return: com.srm.util.ResultObject
     * @Author: wzw
     *
     * @Date: 2019/11/7 13:50
     * @see ISysUserService
     */
    @Override
    public ResultObject insertDeptAndUser(OneToManyDto addDeptAndUserIdDto) {
        String deptId = addDeptAndUserIdDto.getDeptId();
        if (deptId != null) {
            SysRole sysRole = sysRoleService.getSysRole(deptId);
            List<String> userList = addDeptAndUserIdDto.getUserId();
            if (userList != null && userList.size() != 0) {
                for (String s : userList) {
                    SysUser sysUser = new SysUser();
                    sysUser.setUserId(s);
                    sysUser.setRoleCode(deptId);
                    sysUser.setRoleName(sysRole.getRoleName());
                    //修改用户表里的数据
                    sysUserService.updateSysUserSetRoleCode(sysUser);
                    sysUserRoleDao.insertRoleAndUser(UUIDUtil.uuid(), addDeptAndUserIdDto.getDeptId(), s);
                }
                return ResultObject.successReturn(null);
            }
        } else {
            return ResultObject.errorReturn("部门id不能为空");
        }
        return null;
    }

    @Override
    public List<Integer> getDepIdByUserName(String userName) {
        return userDeptPostMapper.getDepIdByUserName(userName);
    }



    @Override
    public PageInfo<Map<String, Object>> getDeptInfo(PageDto pageDto) {
        if (pageDto.getPageNum() == null) {
            pageDto.setPageNum(1);
        }
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        PageInfo<Map<String, Object>> p = new PageInfo<>(userDeptPostMapper.getDeptInfo(pageDto));
        return p;
    }

    @Override
    public int getPersonsInDept(List<String> list) {
        return userDeptPostMapper.getPersonsInDept(list);
    }

    @Override
    public List<Map<String, Object>> getDeptUserByDepts(List<String> list) {
        return userDeptPostMapper.getDeptUserByDepts(list);
    }


    @Override
    public int deleteUserDeptPostByList(List<String> list, String deptId) {
        return userDeptPostMapper.deleteUserDeptPostByList(list,deptId);
    }

    @Override
    public ResultObject getAllUserInfo(String userName) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 功能描述: <br>
     * 〈〉
     *
     * @Param: [personChangreDto]
     * @Return: com.alm.util.ResultObject
     * @Author: wzw
     * @Date: 2019/11/8 13:52
     */
    @Override
    public ResultObject updatePersonInchangre(PersonChangreDto personChangreDto) {
        String deptId = personChangreDto.getDeptId();
        String userId = personChangreDto.getUserId();
        if (deptId != null && userId != null) {
            Integer personInCharge = userDeptPostMapper.getPersonInCharge(deptId);
            //如果有负责人
            if (personInCharge != null) {
                //清空当前部门的负责人
                userDeptPostMapper.updatePersonInCharge(deptId);
                //如果id不同,那么则是更换负责人
                if (!(userId.equals(personInCharge))) {
                    userDeptPostMapper.addPersonInCharge(deptId, userId);
                }
            } else {
                //直接更新负责人
                userDeptPostMapper.addPersonInCharge(deptId, userId);
            }
            return ResultObject.successReturn("更改成功");

        } else {
            return ResultObject.errorReturn("传输数据缺失");
        }
    }
}
